﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Windows;
    using System.Windows.Threading;
    using Microsoft.Phone.Tasks;
    using Microsoft.Samples.WindowsPhoneCloud.StorageClient;

    public class UploadPhotoPageViewModel : BaseViewModel
    {
        private readonly ICloudBlobClient blobClient;
        private readonly Dispatcher dispatcher;

        private string blobName;
        private Uri path;
        private bool isUploading;

        public UploadPhotoPageViewModel()
            : this(App.CloudClientFactory.ResolveCloudBlobClient("UploadAndList"), Deployment.Current.Dispatcher)
        {
        }

        public UploadPhotoPageViewModel(ICloudBlobClient blobClient, Dispatcher dispatcher)
        {
            this.blobClient = blobClient;
            this.dispatcher = dispatcher;
        }

        public string BlobName
        {
            get
            {
                return this.blobName;
            }

            set
            {
                if (this.blobName != value)
                {
                    this.blobName = value;
                    this.NotifyPropertyChanged("BlobName");
                }
            }
        }

        public Uri Path
        {
            get
            {
                return this.path;
            }

            set
            {
                if (this.path != value)
                {
                    this.path = value;
                    this.NotifyPropertyChanged("Path");
                }
            }
        }

        public bool IsUploading
        {
            get
            {
                return this.isUploading;
            }

            set
            {
                if (this.isUploading != value)
                {
                    this.isUploading = value;
                    this.NotifyPropertyChanged("IsUploading");
                }
            }
        }

        public Stream PhotoStream { get; set; }

        public void LoadPhoto(PhotoResult photoResult)
        {
            this.Path = new Uri(photoResult.OriginalFileName);
            this.BlobName = string.Format(
                CultureInfo.InvariantCulture,
                "Capture-{0}{1}",
                DateTime.Now.ToString("yyyyMMddTHHmmss", CultureInfo.InvariantCulture),
                System.IO.Path.GetExtension(photoResult.OriginalFileName));
            this.PhotoStream = photoResult.ChosenPhoto;
        }

        public void UploadPhoto(Action<string> successCallback, Action<string> failureCallback)
        {
            this.IsUploading = true;
            this.blobClient.Upload(
                this.BlobName,
                this.PhotoStream,
                r => this.dispatcher.BeginInvoke(
                        () =>
                        {
                            this.IsUploading = false;

                            if (r.Exception == null)
                            {
                                if (successCallback != null)
                                {
                                    successCallback.Invoke(string.Format(CultureInfo.InvariantCulture, "Image file {0} successfully uploaded!", this.BlobName));
                                }
                            }
                            else
                            {
                                if (failureCallback != null)
                                {
                                    failureCallback.Invoke(string.Format(CultureInfo.InvariantCulture, "Error: {0}", r.Exception.Message));
                                }
                            }
                        }));
        }
    }
}
